/**
 * 
 */
package cn.jhz.filesharingsystem.controller;


import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import cn.jhz.filesharingsystem.model.FileArticle;
import cn.jhz.filesharingsystem.model.Pager;
import cn.jhz.filesharingsystem.model.SystemContext;
import cn.jhz.filesharingsystem.model.User;
import cn.jhz.filesharingsystem.service.SearchServer;
import cn.jhz.filesharingsystem.util.HeadersUtil;
/**
 * @author asus
 *
 */
@Controller
public class SearchController {
	
	@Autowired
	SearchServer searchServer;
	
	@GetMapping(value = {"/main","/"})
	private String searchPage(@RequestHeader(value="User-Agent") String userAgent) {
		if(HeadersUtil.checkAgentIsMobile(userAgent)) {
			return "mobileSearch";
		}
		return "search";
	}
	
	@GetMapping("/search")
	private String getSearchPage(@RequestParam(value = "keywords",required = true) String keywords, @RequestHeader(value="User-Agent") String userAgent
			,@SessionAttribute(value = "loginUser")User loginUser,HttpServletResponse resp) {

		if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
			throw new RuntimeException("帐号已被禁用");
		}
		resp.addCookie(new Cookie("keywords",keywords));
		if(HeadersUtil.checkAgentIsMobile(userAgent)) {
			return "mobileResult";
		}
		return "result";
	}
	
	
	@GetMapping("/search_result")
	private @ResponseBody Pager<FileArticle> search(@RequestParam(value = "page", required = false, defaultValue = "1")Integer page
			   ,@RequestParam(value = "rows", required = false, defaultValue = "10") Integer rows
			   ,@RequestParam(value="keywords",required=true, defaultValue = "") String keywords
			   ,HttpSession session,HttpServletResponse resp) throws ParseException, IOException, InvalidTokenOffsetsException{
		
		Pager<FileArticle> pager = null;
		
		IndexSearcher indexSearcher = (IndexSearcher) session.getAttribute("indexSearcher");
		Query query = (Query) session.getAttribute("query");
		
		/* 设置系统上下文的分页数据 */
		if(page != null && page > 0) SystemContext.setPageOffset((page-1)*rows);
		if(rows != null && rows > 0) SystemContext.setPageSize(rows);

		
		/* 搜索核心代码 */
		if(query != null && indexSearcher !=null && !keywords.equals("")
				&& keywords.equals(session.getAttribute("keywords"))) {
			/* 对相同关键字二次查询时执行 */
			pager = searchServer.getSearchPage(query, indexSearcher);
			
		}else {
			/* 对相同关键字首次查询时执行 */
			query = searchServer.getQuery(keywords);
			//通过searchServer获取查询对象
			indexSearcher = searchServer.getIndexSearcher();
			//通过searchServer获取搜索索引对象
			session.setAttribute("keywords", keywords);
			//将关键字存入session作为下次搜索的判断依据
			session.setAttribute("query", query);
			//将查询对象存入session下次搜索时使用
			session.setAttribute("indexSearcher", indexSearcher);
			//将搜索索引对象存入session下次搜索时使用
			pager = searchServer.getSearchPage(query, indexSearcher);
			//将搜索结果封装到Pager对象
		}		
		
		
		/* 清除系统上下文的分页数据 */
		SystemContext.removePageOffset();
		SystemContext.removePageSize();

		return pager;
	}
	
}
